<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/18
 * Time: 13:21
 */

namespace app\index\controller;


use app\admin\model\Quxiaoorder;
use app\index\lib\Pay;
use app\index\lib\Wxchat;
use app\index\model\WeixiouModel;
use app\lib\SendSms;
use app\lib\SendTemplate;
use app\index\lib\WechatUtil;
use think\Db;
use think\Request;

class Account extends WxBase
{
    //微信图片上传
    public function test(){
        if ($this->request->isAjax()){
            $url = input('url');
            $wxchat =  new Wxchat();
            $accessToken = $wxchat->getAccessToken();//获取accessToken
            $nonceStr = $this->createNonceStr();//获取随机字符串
            $nonceStr = $this->createNonceStr();//获取随机字符串
            $jsapi_ticket = $wxchat->jsapi_ticket($accessToken);//获取到票
            $timestamp = time();
            $string = "jsapi_ticket=$jsapi_ticket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
            $signature = sha1($string);
            $data['appId']     = $wxchat->appId;
            $data['timestamp'] = $timestamp;
            $data['nonceStr']  = $nonceStr;
            $data['signature'] = $signature;
            $signPackage = array('code'=>1,'data'=>$data);

            echo json_encode($signPackage);die;
        }

    }

    private function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    public function wximg(){
        $media_id = input('localData');
        $wxchat =  new Wxchat();
        $accessToken = $wxchat->getAccessToken();//获取accessToken
        $re = $this->getmedia($accessToken,$media_id);
        echo json_encode(['src'=>$re]);die;
    }

    //微信获取远程图片
    private function getmedia($access_token,$media_id,$foldername = 'renzheng'){
        $f = fopen('11.txt', 'wb'); // 打开写入
        fwrite($f,$media_id);
        fclose($f);
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$media_id;
        if (!file_exists("./uploads/User_cert/".date('Ymd').'/'.$foldername)) {
            mkdir("./uploads/User_cert/".date('Ymd').'/'.$foldername, 0777, true);
        }
        $imgname = date('YmdHis').rand(1000,9999);
        $targetName = './uploads/User_cert/'.date('Ymd').'/'.$foldername.'/'.$imgname.'.jpg';

        $ch = curl_init($url); // 初始化
        $fp = fopen($targetName, 'wb'); // 打开写入
        curl_setopt($ch, CURLOPT_FILE, $fp); // 设置输出文件的位置，值是一个资源类型
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);
        return '/uploads/User_cert/'.date('Ymd').'/'.$foldername.'/'.$imgname.'.jpg';;
    }

    //后台首页
    public function index(){

        $tpye1 = db('weixiou')->where(['userid'=>session('userid'), 'type'=>1])->count();
        $tpye2 = db('weixiou')->where(['userid'=>session('userid'), 'type'=>2])->count();
        $tpye3 = db('weixiou')->where(['userid'=>session('userid'), 'type'=>3])->count();
        $tpye4 = db('weixiou')->where(['userid'=>session('userid'), 'type'=>4])->count();
        $total= db('weixiou')->where(['userid'=>session('userid')])->count();
        $order = db('orders')->whereOr('sevrice_id',0)->whereOr('sevrice_id','')->whereOr('sevrice_id',null)->group('order_mun')->select();

        $quxiao = db('quxiaoorder')->where('user_id',session('userid'))->select();
        $arr = array_column($quxiao,'order_mun');
        foreach ($order as $kk=> $vv){
            if (in_array($vv['order_mun'],$arr)){
                unset($order[$kk]);
            }
        }
        if ($total == 0){
            $total = 1;
        }
<<<<<<< HEAD
        $data = ['tpye1'=>$tpye1, 'tpye2'=>$tpye2, 'tpye3'=>$tpye3, 'tpye4'=>$tpye4, 'total'=>$tpye2/$total, 'order'=>count($order)];
=======
        $data = ['tpye1'=>$tpye1, 'tpye2'=>$tpye2, 'tpye3'=>$tpye3, 'tpye4'=>$tpye4, 'total'=>round($tpye2/$total,2), 'order'=>$order];
>>>>>>> 98b4d510f6bc858c89e59222fbe93ccf103e8629
        $this->assign('user',db('member')->where('id',session('userid'))->find());
        $this->assign('tle',db('config')->where('id',1)->field('tile')->find());
        $this->assign('data',$data);
        return $this->fetch();
    }
    //二维码
    public function myimg(){
        $this->assign('userid',session('userid'));
        return $this->fetch();
    }
    //未接订单列表
    public function ontorder(){
        $order = db('orders')
            ->whereOr('sevrice_id',0)
            ->whereOr('sevrice_id','')
            ->whereOr('sevrice_id',null)
            ->group('order_mun')
            ->order('id desc')
            ->field('*,goods_img as original_img,name as addr_name')
            ->select();
        $this->assign('order',$order);
        return $this->fetch();
    }

    //保证金
    public function setmoney(){
        $this->assign('config',db('config')->where('id',1)->field()->find());
        return $this->fetch();
    }
    //钱包列表
    public function getpaylist(){
        $type = input('type')??3;
        $data = db('ticheng')->where(['userid'=>session('userid'),'ticheng'=>$type])->select();
        $this->assign('list',$data);
        $this->assign('type',$type);
        return $this->fetch();
    }
    //设置是否休息
    public function iswork(){
        $iswork = input('iswork')==0?1:0;
        $re = db('member')->where('id',session('userid'))->update(['iswork'=>$iswork]);
        if ($re) {
            $falg = msg(1, ['data'=>$iswork], '');
            return json(['code' => $falg['code'], 'data' => $falg['data'], 'msg' => $falg['msg']]);
        } else {
            $falg = msg(1, ['data'=>$iswork], '');
            return json(['code' => $falg['code'], 'data' => $falg['data'], 'msg' => $falg['msg']]);
        }
    }
    //提现
    public function tixian(){
        $this->assign('money',db('member')->where('id',session('userid'))->find());
        $this->assign('remark',db('config')->where('id',1)->find());
        return $this->fetch();
    }

    /**
     * 余额提现
     * @return array
     */
    public function totixian(){
        $money = input('money');
        $data = db('config')->where('id',1)->find();
        $yue = db('member')->where('id',session('userid'))->find();
        if ($money > $data['money']){
            if ($money > $yue['yue']){
                return ['code'=>-1,'msg'=>"提现金额不能大于余额"];
            }
            $this->qianbao($money,'余额提现',2,3);
            $updata = $yue['yue']-$money;
            $re = db('member')->where('id',session('userid'))->update(['yue'=>$updata]);//更新余额
            if ($re){
                return ['code'=>1,'msg'=>"提现成功!"];
            }
        }else{

            return ['code'=>-1,'msg'=>"提现金额不能低于{$data['money']}"];
        }

    }
    /**
     * 保证金提现
     * @return array
     */
    public function baozhenjintixian(){
        $money = input('money');
//        $data = db('config')->where('id',1)->find();
        $yue = db('member')->where('id',session('userid'))->find();
        if ($money > $yue['baozhenjin']){
            return ['code'=>-1,'msg'=>"提现金额不能大于余额"];
        }
        $this->qianbao($money,'保证金提现',2,4);
        $updata = $yue['baozhenjin']-$money;
        $re = db('member')->where('id',session('userid'))->update(['baozhenjin'=>$updata]);//更新余额
        if ($re){
            return ['code'=>1,'msg'=>"提现成功!"];
        }

    }


    //余额转保证金
    public function baozjin(){
        $money = input('money');
        $data = db('config')->where('id',1)->find();
        $user = db('member')->where('id',session('userid'))->find();
        if ($money > $user['yue']){
            return ['code'=>-1,'msg'=>"转出金额不能大于余额"];
        }else{
            $total = $user['baozhenjin'] +$money;
//            $type = 0;
//            if ($data['a_bail'] <= $total  && $total < $data['a_bail_two']){
//                $type = 1;
//            }elseif ($total >= $data['a_bail_two'] && $total < $data['a_bail_three']){
//                $type = 2;
//            }elseif ($total >= $data['a_bail_three'] && $total < $data['b_bail']){
//                $type = 3;
//            }elseif ( $total >= $data['b_bail']){
//                $type = 4;
//            }
            $this->qianbao($money,'余额转保证金',2,3);//余额减少记录
            $this->qianbao($money,'余额转保证金',1,4);//保证金增加的记录
            $updatayue = $user['yue']-$money;
            //更新用户表的余额，保证金，用户等级
            $re = db('member')->where('id',session('userid'))->update(['baozhenjin'=>$total,'yue'=>$updatayue]);

            if ($re){
                return ['code'=>1,'msg'=>"操作成功!"];
            }
        }

    }
    //添加钱包记录
    public function qianbao($money,$remark,$type,$ticheng){
        $tichengdata = db('ticheng')->where(['userid',session('userid'), 'ticheng' =>$ticheng])->order('id desc')->limit(1)->select();
        $qianbao['userid'] = session('userid');
        $qianbao['type'] = $type;
        $qianbao['remark'] = $remark;
        $qianbao['money'] = $money;
        $qianbao['addtime'] = time();
        $qianbao['ticheng'] = $ticheng;
        $qianbao['after'] = $tichengdata[0]['after'] - $money;//求改变后的量
        //添加钱包记录
        $re = db('ticheng')->where('userid',session('userid'))->insertGetId($qianbao);
        if($re){
            return true;
        }else{
            return false;
        }

    }
    //ajax地区
    public function area($pid=0,$level=0){
        $data =  db('area')->where('parent_id',$pid)->select();
        $user = db('member')->where('id',session('userid'))->find();
        $userarea = explode(',',$user['xian']);
        $html = '';
        foreach ($data as $v){
            if ($v['depth'] == 3){
                foreach ($userarea as $vv){
                    $check = '';
                    if ($v['id'] == $vv){
                        $check = 'checked';
                        break;
                    }
                }
                $html .= '<li style="color: #000" class=" mui-input-row mui-checkbox mui-right tocheckbox">
                            <label>'.$v['name'].'</label>
                            <input name="checkbox"  value="'.$v['id'].'" type="checkbox" '.$check.'>
                        </li>';
            }else{
                $html .= '<li  class="mui-input-row mui-checkbox mui-right tocheckbox" style="color: #000" data-index="2" data-title="area" data-id="'.$v['id'].'">
                <label>'.$v['name'].'</label>
                </li>';
            }

        }
        return ['level'=>$level,'data'=>$html];
    }
    public function addarea(){
        $user = db('member')->where('id',session('userid'))->find();
        $area_name = \db('area')->where('id','in',$user['xian'])->select();
        $arr = ['','审核中','通审核','待认证','通过认证'];
        $this->assign('userarea',json_encode($user['xian']));
        $this->assign('area_name',$area_name);
        $this->assign('step',$arr[$user['step']]);
        return $this->fetch();
    }

    public function areaId(){
        $input = input();
        $user = db('member')->where('id',session('userid'))->find();
        $updata = ['xian'=>implode(',',$input['areaid'])];
        if ($user['step'] == 2){
            $updata['step'] = 1;
        }elseif ($user['step'] == 4){
            $updata['step'] = 3;
        }
        $re = db('member')->where('id',session('userid'))->update($updata);
        if (!$re){
            return ['code'=>-1,'msg'=>'添加失败'];
        }
        return ['code'=>1,'msg'=>'添加成功'];
    }

/////////////////////////////////师傅已接订单
    public function  hasoerder(){
        $where = [];
        $type = input('type')?input('type'):0;
        $data = (new WeixiouModel())->hasoerder();
        $this->assign('all',$data);
        $this->assign('type',$type);
        return $this->fetch();
    }
////////////////////////////////////添加配件信息
    public function addweixiou(){
        $input = $this->request->param();
        if ($this->request->isPost()){

<<<<<<< HEAD
            $peijianinfo = Db::name('weixiou_j')->where('orderid',$input['orderid'])->find();
            Db::name('orders')->where('order_mun',$input['orderid'])->update(['status'=>3]);
            $input['new_oreder_mun'] = new_oreder_mun();
            if (empty($peijianinfo)){
=======
        if ($this->request->isAjax()){
            $input = $this->request->post();
>>>>>>> 98b4d510f6bc858c89e59222fbe93ccf103e8629
                $input['addtime'] = time();
                $input['new_oreder_mun'] = time();
                $re = Db::name('weixiou_j')->insertGetId($input);
                if ($re){
                    return ['code'=>1,'msg'=>'添加成功','url'=>url('topany/topay',['order_mun'=>$input['orderid']])];
                }else{
                    return ['code'=>-1,'msg'=>'添加失败'];
                }

        }
        $peijianinfo = Db::name('weixiou_j')->where('orderid',input('id'))->find();
        if ($peijianinfo){
<<<<<<< HEAD
            if (isset($input['xiougao']) && !empty($input['xiougao'])){

            }else{
                $this->redirect('topany/topay',['order_mun'=>$peijianinfo['orderid']]);
            }

        }
        $this->assign('id',input('id'));
        $this->assign('order',db('orders')->where('order_mun',input('id'))->find());
        $this->assign('order_j',db('weixiou_j')->where('orderid',input('id'))->find());
=======
            $this->redirect('index/Topany/topay',['order_mun'=>input('id')]);
        }
        $this->assign('id',input('id'));
        $this->assign('order',db('orders')->where('order_mun',input('id'))->find());
>>>>>>> 98b4d510f6bc858c89e59222fbe93ccf103e8629
        return $this->fetch();
    }

    public function editweixiou(){
        if ($this->request->isAjax()){
            $input = $this->request->post();
            $input['new_oreder_mun'] = time();
            $re = Db::name('weixiou_j')->where('orderid',$input['orderid'])->update($input);
            if ($re){
                return ['code'=>1,'msg'=>'添加成功','url'=>url('topany/topay',['order_mun'=>$input['orderid']])];
            }else{
                return ['code'=>-1,'msg'=>'添加失败'];
            }
        }
        $data = Db::name('weixiou_j')->where('orderid',input('orderid'))->find();
        $this->assign('data',$data);
        $this->assign('order',db('orders')->where('order_mun',input('orderid'))->find());
        return $this->fetch();

    }
////////////////////////////////////////////服务商分配订单
   public function touserorder(Request $request){
       if($request->isAjax()){
           $id = input('id');
           $data['quxiao_remake'] = input('quxiao_remake');
           $data['order_mun'] = $id;
           $data['type'] = 2;
           $data['user_id'] = session('userid');
           $data['addtime'] = time();
           $re = db('quxiaoorder')->insertGetId($data);
           db('orders')->where('order_mun',$id)->update(['sevrice_id'=>0]);
           db('weixiou')->where('orderid',$id)->update(['type'=>3]);
           if ($re) {
               $user = DB::name('member')->where('id',session('userid'))->find();
               (new SendTemplate($id))->toDoSend(1,$user['wxopenid']);//发送模板信息
               return ['code' =>1, 'msg' => '转让成功'];
           } else {
               return ['code' =>-1, 'msg' => '转让失败'];
           }
       }

       $config = db('config')->where('id',1)->find();
       $sf_quxiao = [];
       if ($config['zhuanrang_quxiao']){
           $sf_quxiao = explode(',',$config['zhuanrang_quxiao']);
       }
       $this->assign('order_mun',input('id'));
       $this->assign('sf_quxiao',$sf_quxiao);
       return $this->fetch();

   }
    //取消接单 如果次数小于三则转发订单
    public function todelorder(Request $request){
        $config = db('config')->where('id',1)->find();
       if($request->isAjax()){
           $id = input('id');
           $updata['sevrice_id'] = 0;
           $cishu = db('orders')->where('order_mun',$id)->field('cishu')->find();
           if ($cishu['cishu'] <$config['zhuanrang']+1){
               $updata['cishu'] = $cishu['cishu']+1;
           }
           $data['quxiao_remake'] = input('quxiao_remake');
           $data['order_mun'] = $id;
           $data['type'] = 1;
           $data['user_id'] = session('userid');
           $data['addtime'] = time();
           $re =db('quxiaoorder')->insertGetId($data);
           db('orders')->where('order_mun',$id)->update($updata);
           db('weixiou')->where(['orderid'=>$id,'userid'=>session('userid')])->update(['type'=>4]);
           if ($re) {
               if ($cishu['cishu'] <$config['zhuanrang']+1) {
                    $user = DB::name('member')->where('id',session('userid'))->find();
                   (new SendTemplate($id))->toDoSend(1,$user['wxopenid']);//发送模板信息
               }
               return ['code' =>1, 'msg' => '取消成功'];
           } else {
               return ['code' =>-1, 'msg' => '取消失败'];
           }
       }
        $sf_quxiao = [];
       if ($config['sf_quxiao']){
           $sf_quxiao = explode(',',$config['sf_quxiao']);
       }
        $this->assign('order_mun',input('id'));
        $this->assign('sf_quxiao',$sf_quxiao);
       return $this->fetch();

    }

    //师傅报名
    public function jionme(){

        if (request()->isAjax()) {
            $input = input();
            //            没有做完 应该是添加
            $re = db('member')->where('id',session('userid'))->update([
                                                                                                'fenleiid' => implode(',',$input['fenleiid']),
                                                                                                'phone' => input('phone'),
                                                                                                'mobile' => input('phone1'),
                                                                                                'step'=>1,
                                                                                            ]);
            if ($re) {
                $falg = msg(1, [], '申请成功');
                return json(['code' => $falg['code'], 'data' => $falg['data'], 'msg' => $falg['msg']]);
            } else {
                $falg = msg(1, [], '申请失败');
                return json(['code' => $falg['code'], 'data' => $falg['data'], 'msg' => $falg['msg']]);
            }
        }
        $arr = ['','审核中','通审核','待认证','通过认证'];
        $jineng = Db::name('member')->where('id',input('userid'))->find();
        $this->assign('jineng',explode(',',$jineng['fenleiid']));
        $this->assign('userid',input('userid'));
        $this->assign('step',$arr[$jineng['step']]);
        $this->assign('menu',db('fenlei')->where('pid',0)->select());
        $this->assign('config',db('config')->where('id',1)->find());

        return $this->fetch();
    }
    //用户认证
    public function toauth(){
        $config = db('config')->where('id',1)->find();
        if (request()->isAjax()) {
            $param = input();
//            $area_id = explode(',',$param['area_id']);
            $re = db('member')->where('id',session('userid'))->update([
                                                                                        'real_name' => $param['realname'],
                                                                                        'phone' => $param['phone'],
                                                                                        'mobile' => $param['phone1'],
                                                                                        'identity' => $param['identity'],
                                                                                        'img' => $param['img1'],
                                                                                        'imgs' => $param['img3'],
                                                                                        'type'=>1,
                                                                                        'group'=>input('type'),
//                                                                                        'step'=>3,
                                                                                    ]);
            if ($re){
                $money =0;
                if (input('type') ==1){
                    $money = $config['a_bail'];
                }elseif (input('type')==2){
                    $money = $config['a_bail_two'];
                }elseif (input('type')==3){
                    $money = $config['a_bail_three'];
                }
                return json(msg(1, ['money'=>$money], '申请成功'));
            }else{
                return json(msg(-1, '', '未知错误'));
            }

        }
        $user = db('member')->where(['id'=>session('userid'),'type'=>1])->find();
//        $user['img'] = explode(',',$user['img']);

        $this->assign('status',1);
        $this->assign('userid',input('userid'));
        $this->assign('config',$config);
        $this->assign('users',$user);
        return $this->fetch();
    }
    //服务商认证
    public function fuwushang(){
        $config = db('config')->where('id',1)->find();
        if (request()->isAjax()) {
            $param = input();
//            $area_id = explode(',',$param['area_id']);
            $re = db('member')->where('id',session('userid'))->update([
                                                                                            'real_name' => $param['realname'],
                                                                                            'phone' => $param['phone'],
                                                                                            'mobile' => $param['phone1'],
                                                                                            'identity' => $param['identity'],
                                                                                            'img' => $param['img1'],
                                                                                            'imgs' => $param['img2'],
                                                                                             'type'=>2,
                                                                                             'group'=>input('type'),
//                                                                                             'step'=>3,
                                                                                        ]);
            if ($re){
                $money = $config['b_bail'];
                return json(msg(1, ['money'=>$money], '申请成功'));
            }else{
                return json(msg(-1, '', '未知错误'));
            }
        }
        $this->assign('status',2);
        $this->assign('userid',input('userid'));
        $this->assign('config',$config);
        $this->assign('users',db('member')->where(['id'=>session('userid'),'type'=>2])->find());
        return $this->fetch();
    }

    //上传图片
    public function uplaodimg(){

        if(request()->isAjax()){

            $file = input('file');
            if (strstr($file,",")){
                $image = explode(',',$file);
                $arr = explode(';',explode('/',$image[0])[1]);
                $image = $image[1];
            }

            $imageName = "25220_".date("His",time())."_".rand(1111,9999).'.'.$arr[0];
            // 移动到框架应用根目录/public/uploads/ 目录下
            $path = ROOT_PATH . 'public' . DS . 'upload';
            $imageSrc= $path."/". $imageName;
            $info = file_put_contents($imageSrc, base64_decode($image));

            if($info){
                $src =  '/upload' . '/' .$imageName;
                return json(msg(1, ['src' => $src], '上传成功'));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }
    public function getarea(){
        $menu =db('area')->field('id as value,name as text,parent_id as pid')->select();
        $data = $this->totree($menu); //print_r($data);
        $falg = msg(1, $data,'ok');
        return json(['code'=>$falg['code'],'data'=>$falg['data'],'msg'=>$falg['msg']]);
    }
    private function totree($menu){
        $arr = [];
        foreach($menu as $key => $vo){
            $res[$vo['value']] = $vo;
            $res[$vo['value']]['children'] = [];
        }
        unset($menu);
        foreach ($res as $key => $vo){
            if(0 != $vo['pid']){
                $res[$vo['pid']]['children'][] = &$res[$key];
            }
        }
        //过滤杂质
        foreach( $res as $key=>$vo ){
            if(0 == $vo['pid']){
                $tree[] = $vo;
            }
        }
        unset( $res );

        return $tree;
    }

    /**
     * 网页支付
     * @return mixed
     */
    public function todopay(){
        $redirectUri = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
//
        $payObj = new Pay($redirectUri);
        $authInfo = $payObj->getAuthInfo();
        if(!empty($authInfo['openId'])){
            $param = input();
            session(session('userid').'_baozhengjin',$param['money'],3600);
            $payInfo = array(
                'body'=>'会员认证',
                'bianhao'=>date('Ymdhis',time()).rand(1000, 10000),
                'fee'=>$param['money'],
                'openId'=>$authInfo['openId'],
            );
            $payRes = $payObj->toPay($payInfo);
            $this->assign('payRes',$payRes);
            $this->assign('payInfo',$payInfo);

        }
        return $this->fetch();
    }
    //异步通知会员认证
    public function notify(){
        $_baozhengjin = session(session('userid').'_baozhengjin');
        $user = db('member')->where('id',session('userid'))->find();
        $baozhengjin = $_baozhengjin + $user['baozhenjin'];
        $re = db('member')->where('id',session('userid'))->update(['step'=>3,'baozjin'=>1,'baozhenjin'=>$baozhengjin ]);//将用户修改为待审状态
        if ($re){
            $this->qianbao($_baozhengjin,'保证金充值',1,4);
            $this->redirect(url('index'));
        }
    }

    public function baozjinview(Request $request){

        $this->assign('money',db('member')->where('id',session('userid'))->find());
        $this->assign('remark',db('config')->where('id',1)->find());
        return $this->fetch();
    }

    /**
     * 接单页面
     * @return mixed
     */
    public function jiedan(){
        $user = db('member')->where('id',session('userid'))->find();

        $config = db('config')->where('id',1)->find();
        if ($user['prohibit'] == 2){
            echo "您已经被禁止接单！请联系管理员开通:";die;
        }
        $input = input();
        $data = db('orders') ->where('order_mun',$input['order_mun'])->select();
        $weixiou = db('weixiou')->where(['type'=>1, 'userid'=>session('userid')])->count('id');
        $count = 0; //判断是否有未完成订单
        $time =0;//距离抢单时间
        $xianzhi = 0;//不同等级下抢单次数限制

        if ($user['group'] == 1){//初级师傅
            if ($config['acount'] > 0){
                if ($weixiou > $config['acount']){
                    $count = 1;
                    $xianzhi = 1;
                }
            }
//            if (!empty($weixiou)){
//                $count = 1;
//            }
            if ($config['atime'] > 0){
                $time =$config['atime']+$data[0]['a_time'];
            }
        }elseif ($user['group'] == 2){//中级师傅
            if ($config['acount_two'] > 0){
                if ($weixiou > $config['acount_two']){
                    $count = 1;
                    $xianzhi = 1;
                }
            }
//            if (!empty($weixiou)){
//                $count = 1;
//            }
            if ($config['atime_two'] > 0){
                $time =$config['atime_two']+$data[0]['a_time'];
            }
        }elseif ($user['group'] == 3){//高级
            if ($config['acount_three'] > 0){
                if ($weixiou > $config['acount_three']){
                    $xianzhi = 1;
                    $count = 1;
                }
            }
//            if (!empty($weixiou)){
//                $count = 1;
//            }
            if ($config['atime_three'] > 0){
                $time =$config['atime_three']+$data[0]['a_time'];
            }
        }elseif ($user['group'] == 4){//服务商
            if ($config['bcount'] > 0){
                if ($weixiou > $config['bcount']){
//                    $xianzhi = 1;
//                    $count = 1;
                }
            }
//            if (!empty($weixiou)){
//                $count = 1;
//            }
            if ($config['btiame'] > 0){
                $time =$config['btiame']+$data[0]['a_time'];
            }
        }elseif ($user['group'] == 5){//0元保证金
            if ($config['baozj_0_count'] > 0){
                if ($weixiou > $config['baozj_0_count']){
                    $xianzhi = 1;$count = 1;
                }
            }
//            if (!empty($weixiou)){
//                $count = 1;
//            }
            if ($config['baozj_time'] > 0){
                $time =$config['baozj_time']+$data[0]['a_time'];
            }
        }else{
            echo "您还不是服务商！";die;
        }

        $arr = array_column($data,'goods_name');
        $this->assign([
            'data'=>$data,
            'arr'=>$arr,
            'count'=>$count,
            'time'=>$time,
            'xianzhi'=>$xianzhi,
            'zhuanrang'=>Quxiaoorder::get(function ($q){
                $q->where('id',Quxiaoorder::where(['order_mun'=>input('order_mun')])->max('id'));
            }),
        ]);
        return $this->fetch();
    }

    /**
     * 接单
     * @return array
     */
    public function todoqiandan(){
        $input = input();
        $order = db('orders')->where('order_mun',$input['order_mun'])->select();
        if(!empty($order[0]['sevrice_id'])){
            return ['code'=>-1,'msg'=>'您晚了，订单已被抢走！'];
        }

        db('member')->where('id',session('userid'))->update(['count'=>session('count')+1]);
        $order = db('orders')->where('order_mun',$input['order_mun'])->update(['sevrice_id'=>session('userid')]);
        db('weixiou')->insertGetId(['orderid'=>$input['order_mun'],'userid'=>session('userid'),'addtime'=>time()]);
        if ($order){
            return ['code'=>1,'msg'=>'抢单成功'];
        }else{
            return ['code'=>-2,'msg'=>'网络错误！'];
        }

    }
    public function problem(){
        $problem = db('config')->where('id',1)->find();
        $this->assign('problem',$problem);
        return $this->fetch();
    }

    public function send(){
        (new SendSms(13640956106))->toSendCode();
    }

}

